package com.jianyun.jyzs.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmSearchBean;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.widget.JYWebView;
import com.jianyun.jyzs.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by yangshijun on 2018/6/4.
 * 客户详情
 */

public class CustomerDetialFragment extends Fragment {

    @BindView(R.id.rc_webview)
    JYWebView webView;
    private LoadingDialog loadingDialog;
    private String url;
    private String strUrl = "SCSEXEC/DetailRecordPhone.aspx?TblName=crm_customerinfo|maintbl=crm_customerinfo|htmlIndex=100|mainid=";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.rc_ac_webview, null);
        ButterKnife.bind(this, view);
        initData();
        return view;
    }

    private void initData() {
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        String userId = LoginCache.getInstance().getUserInfo().getUserId();
        CrmSearchBean.ListCrm poBean = (CrmSearchBean.ListCrm) getArguments().getSerializable(SysConstant.msg_key_parcelable);
        loadingDialog = LoadingDialog.create(getContext());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.setWebViewClient(new OAWebViewClient());
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDisplayZoomControls(false);
        url = erpRootUrl + "JiChengLogin.aspx" + "?userid=" + userId + "&urlStr=" + strUrl + poBean.getAutoid() + "|hidsql=autoid=" + poBean.getAutoid();
        RceLog.d("lml", url);
    }

    public void loadUrl(String url) {
        if (webView!=null){
            webView.loadUrl(url);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadUrl(url);
    }

    class OAWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            loadingDialog.show();

            //view.loadUrl(url);
            RceLog.d("lml", "loading..." + url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            loadingDialog.dismiss();
        }
    }

}
